package controller.customer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import controller.DoCheck;
import lib.MyUtils;
import model.Customer;
import view.customer.CustomerMainPanel;
import dao.CustomerDAO;

public class CustomerControl implements ActionListener, MouseListener {
	private CustomerDAO cusDAO;
	private CustomerMainPanel cus;
	private DoCheck check;

	public CustomerControl(CustomerMainPanel cus) {
		this.cus = cus;
		cusDAO = new CustomerDAO();
		check = new DoCheck();
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if (event.getSource() == cus.getBtnCusAdd()) {
			doSave(event);
		} else if (event.getSource() == cus.getBtnCusEdit()) {
			cus.getBtnCusUpdate().setEnabled(true);
		} else if (event.getSource() == cus.getBtnCusDelete()) {
			doDelete(event);
		} else if (event.getSource() == cus.getBtnCusUpdate()) {
			doUpdate(event);
		} else if (event.getSource() == cus.getBtnCusSearch()) {
			doSearch(event);
		} else if (event.getSource() == cus.getBtnCusCancel()) {
			doReset(event);
		}
	}

	private void doUpdate(ActionEvent event) {
		String cusId = (String) cus.getTblCusListcustomer().getValueAt(cus.getTblCusListcustomer().getSelectedRow(),0);
		Customer customer = cusDAO.searchCustomer(cusId);
		
		String name = cus.getTxtCusPersonalID().getText();
		String address = cus.getTxtaCusAddress().getText();
		String email = cus.getTxtCusEmail().getText();
		String phone = cus.getTxtCusPhone().getText();
		
		customer.setName(name);
		customer.setPhone(phone);
		customer.setEmail(email);
		customer.setAddress(address);
		
		if(cusDAO.updateCustomer(customer)&& check.doCheckPhone(phone)&& DoCheck.doCheckEmail(email)){
			JOptionPane.showMessageDialog(null, "Update Customer successfull ");
			doLoad(event);
		}else{
			JOptionPane.showMessageDialog(null, "Update Customer fail");
		}
	}

	private void doReset(ActionEvent event) {
		cus.getTxtCusName().setText("");
		cus.getTxtaCusAddress().setText("");
		cus.getTxtCusEmail().setText("");
		cus.getTxtCusPersonalID().setText("");
		cus.getTxtCusPhone().setText("");
		cus.getTxtCusSearch().setText("");

		cus.getBtnCusEdit().setEnabled(false);
		cus.getBtnCusUpdate().setEnabled(false);
		cus.getBtnCusDelete().setEnabled(false);
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		String id = (String) cus.getTblCusListcustomer().getValueAt(
				cus.getTblCusListcustomer().getSelectedRow(), 0);
		Customer customer = (Customer) cusDAO.searchCustomer(id);
		cus.getTxtCusName().setText(customer.getId());
		cus.getTxtCusPersonalID().setText(customer.getName());
		cus.getTxtaCusAddress().setText(customer.getAddress());
		cus.getTxtCusEmail().setText(customer.getEmail());
		cus.getTxtCusPhone().setText(customer.getPhone());
		cus.getBtnCusEdit().setEnabled(true);
		cus.getBtnCusDelete().setEnabled(true);

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {

	}

	@Override
	public void mouseExited(MouseEvent arg0) {

	}

	@Override
	public void mousePressed(MouseEvent arg0) {

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {

	}

	@SuppressWarnings("static-access")
	public void doSave(ActionEvent event) {
		Customer customer = new Customer();

		String cusID = cus.getTxtCusName().getText();
		customer.setId(cusID);

		String cusName = cus.getTxtCusPersonalID().getText();
		customer.setName(cusName);

		String cusEmail = cus.getTxtCusEmail().getText();
		customer.setEmail(cusEmail);

		String cusPhone = cus.getTxtCusPhone().getText();
		customer.setPhone(cusPhone);

		String cusAdd = cus.getTxtaCusAddress().getText();
		customer.setAddress(cusAdd);

		if (check.doCheckEmail(cusEmail) && check.doCheckPerId(cusID)
				&& check.doCheckPhone(cusPhone)
				&& cusDAO.insertCustomer(customer)) {
			JOptionPane.showMessageDialog(null, "Insert Customer successfull!");
			doLoad(event);

		} else {
			JOptionPane.showMessageDialog(null, "Insert Customer fail!");
		}

	}

	public void doDelete(ActionEvent event) {
		int check = JOptionPane.showConfirmDialog(null,
				"Do you want to delete?");
		if (check == JOptionPane.YES_OPTION) {
			String cusId = (String) cus.getTblCusListcustomer().getValueAt(
					cus.getTblCusListcustomer().getSelectedRow(), 0);
			cusDAO.deleteCustomer(cusDAO.searchCustomer(cusId));
			JOptionPane.showMessageDialog(null, "delete customer successfully");
			doLoad(event);

		} else if (check == JOptionPane.CANCEL_OPTION) {
		}
	}



	public void doLoad(ActionEvent event) {
		MyUtils.doLoadCustomer(cus);
	}

	public void doSearch(ActionEvent event) {
		String key = cus.getTxtCusSearch().getText();
		DefaultTableModel dtm = new DefaultTableModel();
		dtm.addColumn("ID");
		dtm.addColumn("Name");
		dtm.addColumn("Phone");
		dtm.addColumn("Email");
		dtm.addColumn("Address");
		for (Customer customer : cusDAO.showAllCustomerByAll(key)) {
			dtm.addRow(new Object[] { customer.getId(), customer.getName(),
					customer.getPhone(), customer.getEmail(),
					customer.getAddress() });
		}
		cus.getTblCusListcustomer().setModel(dtm);

	}

}
